package com.ailargemodel.controller;

import com.ailargemodel.common.BaseResponse;
import com.ailargemodel.common.ErrorCode;
import com.ailargemodel.common.utils.ResultUtil;
import com.ailargemodel.pojo.User;
import com.ailargemodel.service.UserService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@CrossOrigin("*")
@RestController
@RequestMapping("/AIUser")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/loginandregister")
    public BaseResponse loginandregister(@RequestBody User user, HttpServletRequest request, HttpServletResponse response) throws NoApiKeyException, InputRequiredException {
        if (user == null || StringUtils.isAnyBlank(user.getName(), user.getStudyId())) {
            return ResultUtil.error(ErrorCode.VARIABLE_EMPTY_ERROR);
        }
        return userService.loginandregister(user.getName(),user.getStudyId(), request, response);
    }

    @PostMapping("/logout")
    public BaseResponse logOut(@RequestBody String studyId, HttpServletRequest request, HttpServletResponse response, @CookieValue(value = "loginCookie", defaultValue = "") String cookie) {
        if (studyId == null || studyId == "") {
            return ResultUtil.error(ErrorCode.VARIABLE_EMPTY_ERROR);
        }
//        return ResultUtil.success(userService.userLogout(userId, request, response, cookie));
        return ResultUtil.sucess(userService.userLogOut(studyId, request, response, cookie));
    }

}
